package cn.leedsoft.and.crimeintent.model;

import android.content.Context;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * Created by leeds on 2016/8/8 9:15.
 */
public class CrimeLab {
    private static CrimeLab sCrimeLab;
    private Context mContext;
    private List<Crime> mCrimes;
    private static final String SAVE_TO_FILE_NAME = "crimes.json";

    public List<Crime> getCrimes() {
        return mCrimes;
    }

    private CrimeLab(Context context) {
        mContext = context;

        mCrimes = loadCrimes(SAVE_TO_FILE_NAME);
    }

    private List<Crime> loadCrimes(String filename) {
        List<Crime> res = null;
        BufferedReader reader = null;
        try {
            FileInputStream fis = mContext.openFileInput(filename);//写入文件
            reader = new BufferedReader(new InputStreamReader(fis));
            StringBuilder crimesJson = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                crimesJson.append(line);
            }
            res = JSON.parseArray(crimesJson.toString(), Crime.class);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (res == null) {
                res = Lists.newArrayList();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return res;
    }

    public void saveCrimes() {
        String toSaveJson = JSON.toJSONString(mCrimes);
        BufferedWriter writer = null;
        try {
            FileOutputStream fos = mContext.openFileOutput(SAVE_TO_FILE_NAME, Context.MODE_PRIVATE);//读取文件
            writer = new BufferedWriter(new OutputStreamWriter(fos));
            writer.write(toSaveJson);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void addCrime(Crime c) {
        mCrimes.add(c);
    }

    public void delCrime(Crime c) {
        if(c == null) return;
        mCrimes.remove(c);
    }

    public Crime get(UUID uuid) {
        for (Crime crime : mCrimes) {
            if (crime.getId().equals(uuid))
                return crime;
        }
        return null;
    }

    public static CrimeLab getInstance(Context context) {
        if (sCrimeLab == null) {
            sCrimeLab = new CrimeLab(context);
        }
        return sCrimeLab;
    }
}
